<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\index\controller;

use think\Controller;
use think\Db;
use app\index\model\UsersModel;
use app\index\model\SmsLogModel;

class Login extends Controller
{
    // 登录页面
    public function index(){
        $param=input('param.');
        if(!isset($param['user_id'])){
            return json(msg(-1, '', '参数错误'));
        }
        $usersModel = new UsersModel();
        $hasUser = $usersModel->findUserById($param['user_id']);
        if(empty($hasUser)){
            return json(msg(-2, '', '用户不存在'));
        }else{
            return json($usersModel->isLock($hasUser));
        }
        //return json(msg(1,'','登录验证成功'));
    }
    // 注册操作
    public function reg(){
        if(request()->isPost()) {
            $param = input('param.');
            //$check = paramCheck($param, 'mobile,password,code,session_id');
            $check = paramCheck($param, 'mobile,password,code');
            if ($check['code'] != 1) return json($check);
            if (!check_mobile($param['mobile'])) return json(msg(-1, '', '手机号格式不正确'));
            $model = new UsersModel();
            $ret = $model->register($param);
            return json($ret);
        }
        return $this->fetch();
    }
    // 登录操作
    public function login(){
        if(request()->isPost()) {
            $param = input('param.');
            $check = paramCheck($param, 'mobile,password');
            if ($check['code'] != 1) {
                return json($check);
            }
            $model = new UsersModel();
            $ret = $model->login($param);
            return json($ret);
        }
        return $this->fetch();
    }

    //忘记密码,修改密码
    public function modifyPassword(){
        $param=input('param.');
        $check=paramCheck($param,'mobile,password,password2,code,session_id');
        if($check['code']!=1){
            return json($check);
        }
        $model=new UsersModel();
        $ret=$model->modifyPassword($param);
        return json($ret);
    }
    //发送短信验证码
    public function sendCode(){
        //短信场景
        $param=input("param.");
        $smsLog=new SmsLogModel();
        return json($smsLog->sendMobileVerifyCode($param));
    }
}